/*************************************************************************************************************
Trigger for the Closure of Duplicate Leads when the master lead is converted
    Author                 Date                   Version                Update
Arun Ramachandran          2nd Sept 2011          V1.1                
Mishra Bibhudutta          2nd Sept 2011          V1.2                   Update ISCONVERTED command to identify the converted lead.
Arun Ramachandran          28th Sept 2011         V1.3                   Updated requirment 734 to Update Last Disposition field value
Bibhudutta Mishra          28th Sept 2011         V1.4                   Added WinBack Logic
Arun Ramachandran          29th Sept 2011         V1.5                   Seperated logic for Winback account and normal account  
**************************************************************************************************************/
trigger OORDuplicateLeadClosure on Lead (after update){

                  
              set<string> leaduniqueset = new set<string>();
              ID LeadRecordTypeID = RecordTypeHelper.GetRecordTypeID('Lead', 'Local Advertiser');
              List<Lead> LeadList = new List<Lead>();
              set<Id> leadWinBack = new set<Id>();
              List<Lead> WinBackLeadlist = new List<Lead>();
           
           //Code block to identify the Converted Lead
              for(Lead lead1:System.Trigger.new) {
                     if(lead1.Unique_Lead_list__c!=null && lead1.ISCONVERTED==TRUE && lead1.RecordtypeId==LeadRecordTypeID && lead1.Win_Back_Account__c==null){
                         leaduniqueset.add(lead1.Unique_lead_list__c);
                         //system.debug('Normal Lead Account Set'+leaduniqueset);
                     }
                     if(lead1.Win_Back_Account__c!=null && lead1.ISCONVERTED==TRUE && lead1.RecordtypeId==LeadRecordTypeID){
                         leadWinBack.add(lead1.Win_Back_Account__c);
                         //system.debug('Winback Lead Account Set'+leadWinBack);

                     }

                 }
                 
              //Query to find out all the Leads with the same Unique Lead List value   
              if(leaduniqueset.size()>0){
              LeadList = [select id,Unique_lead_list__c,Lead_Status_Readonly__c,Status from Lead where Unique_lead_list__c IN :leaduniqueset and Unique_lead_list__c != null AND ISCONVERTED = FALSE];
              }
              if(leadWinBack.size()>0){
              WinBackLeadlist = [select id,Unique_lead_list__c,Lead_Status_Readonly__c,Status from Lead where Win_Back_Account__c IN :leadWinBack and Win_Back_Account__c !=null AND ISCONVERTED = FALSE];
              }
              //Code block to modify the Lead status of the duplicate or those leads who share the same Unique Lead List value to Invalid Leads. 
              if (LeadList.size()>0){
                 for(Lead ld:Leadlist){
                      ld.Status = 'Invalid Lead';
                      ld.Lead_Status_Readonly__c = 'Invalid Lead';
                      ld.Disposition_Reason__c = 'Duplicate Lead Sold';
                      ld.Disposition__c = 'Invalid';    // Update added for V1.3
             }
             }
              if(WinBackLeadlist.size()>0){
                 for(Lead ad: WinBackLeadlist){
                      ad.Status = 'Invalid Lead';
                      ad.Lead_Status_Readonly__c = 'Invalid Lead';
                      ad.Disposition_Reason__c = 'Duplicate Lead Sold';
                      ad.Disposition__c = 'Invalid';
                  }
              
              }   
             If(Leadlist.size()>0){
                 try{
                     Update LeadList;
                                         }
                 Catch(DMLexception e){
                     GeneralConstants.mailAnyExceptionToMe('OORDuplicateLeadClosure','Trigger on Lead',e);
                    }
                    
             }
            If(WinBackLeadlist.size()>0){
                 try{
                     Update WinBackLeadlist;
                                         }
                 Catch(DMLexception e){
                     GeneralConstants.mailAnyExceptionToMe('OORDuplicateLeadClosure','Trigger on Lead',e);
                     }
             }
}